<?php
use lib\Rest;
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件


// 模拟 http 请求
function https_request($url,$data = null)
{
	// php curl 发起get或者post请求
	// curl 初始化
	$curl = curl_init();	// curl 设置
	curl_setopt($curl, CURLOPT_URL, $url);  
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
	
	// 判断 $data get  or post
	if ( !empty($data) ) {
		curl_setopt($curl, CURLOPT_POST, 1);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
	}

	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

	// 执行
	$res = curl_exec($curl);
	curl_close($curl);
	return $res;

}


//qq登录
function qqlogin($code)
{
	$token_url ='https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=101472874&client_secret=d62b7e79baf43ee60606c63e7170003b&code='.$code.'&redirect_uri=http://www.yunxiangyishang.xin/mylogin';
	$token_res = https_request($token_url);
	// $token_res ='access_token=62B9CA7A22C17F744927923D0B74E3B3&expires_in=7776000&refresh_token=EA4A9DDA37B8EA5E7034A7C98C874EDE';
	$data = explode('&',$token_res);
	$data = explode('=',$data[0]);
	$token = $data[1];
	// var_dump($token);die;
	// Step4：使用Access Token来获取用户的OpenID
	$openid_url = 'https://graph.qq.com/oauth2.0/me?access_token='.$token;
	$open_res = https_request($openid_url);
	$lpos = strpos($open_res, "(");
	$rpos = strrpos($open_res, ")");
	$open_res  = substr($open_res, $lpos + 1, $rpos - $lpos -1);
	$msg = json_decode($open_res,true);
	// var_dump($msg);die;
	$openid = $msg['openid'];

	// var_dump($openid);die;
	// Step5：使用Access Token以及OpenID来访问和修改用户数据
	$userinfo_url = 'https://graph.qq.com/user/get_user_info?access_token='.$token.'&oauth_consumer_key=101472874&openid='.$openid;
	$userinfo =  https_request($userinfo_url);
	$usermsg = json_decode($userinfo,true);

	// var_dump($usermsg);die;
	$nickname = $usermsg['nickname'];
	// $nickface = $usermsg['figureurl_qq_2'];

	return $qqdata = ['nickname'=>$nickname,'openid'=>$openid];

}

/**
  * 发送模板短信
  * @param to 手机号码集合,用英文逗号分开
  * @param datas 内容数据 格式为数组 例如：array('Marry','Alon')，如不需替换请填 null
  * @param $tempId 模板Id
  */   
function sendTemplateSMS($to,$datas,$tempId)
{

    //主帐号
    $accountSid= '8a216da860bad76d0160d8ea13e00b75';

    //主帐号Token
    $accountToken= 'e56f64b1d4c34f9099e6a4c0d6660529';

    //应用Id
    $appId='8a216da860bad76d0160d8ea14360b7b';

    //请求地址，格式如下，不需要写https://
    $serverIP='app.cloopen.com';

    //请求端口 
    $serverPort='8883';

    //REST版本号
    $softVersion='2013-12-26';

     // 初始化REST SDK
     // global $accountSid,$accountToken,$appId,$serverIP,$serverPort,$softVersion;
     $rest = new Rest($serverIP,$serverPort,$softVersion);
     $rest->setAccount($accountSid,$accountToken);
     $rest->setAppId($appId);
    
     // 发送模板短信
     $result = $rest->sendTemplateSMS($to,$datas,$tempId);
   
}







 ?>